<?php

/**
 * +----------------------------------------------------------------------
 * | 发送验证码
 * |
 * | 比如：
 * |        	<a class="js-get-mobile-code" style="color: #000000;" data-wait-msg="[second]秒后才能再次获取"
 * |			data-need-mobile="请输入手机号" data-mobile-input="#js-mobile-input"
 * |			data-url="cmf_plugin_url('DferThinkcmfTools://ApiSendCode/send')" data-init-second-left="60">获取验证码</a>
 * +----------------------------------------------------------------------
 *                                            ...     .............
 *                                          ..   .:!o&*&&&&&ooooo&; .
 *                                        ..  .!*%*o!;.
 *                                      ..  !*%*!.      ...
 *                                     .  ;$$!.   .....
 *                          ........... .*#&   ...
 *                                     :$$: ...
 *                          .;;;;;;;:::#%      ...
 *                        . *@ooooo&&&#@***&&;.   .
 *                        . *@       .@%.::;&%$*!. . .
 *          ................!@;......$@:      :@@$.
 *                          .@!   ..!@&.:::::::*@@*.:..............
 *        . :!!!!!!!!!!ooooo&@$*%%%*#@&*&&&&&&&*@@$&&&oooooooooooo.
 *        . :!!!!!!!!;;!;;:::@#;::.;@*         *@@o
 *                           @$    &@!.....  .*@@&................
 *          ................:@* .  ##.     .o#@%;
 *                        . &@%..:;@$:;!o&*$#*;  ..
 *                        . ;@@#$$$@#**&o!;:   ..
 *                           :;:: !@;        ..
 *                               ;@*........
 *                       ....   !@* ..
 *                 ......    .!%$! ..     | AUTHOR: dfer
 *         ......        .;o*%*!  .       | EMAIL: df_business@qq.com
 *                .:;;o&***o;.   .        | QQ: 3504725309
 *        .;;!o&****&&o;:.    ..          | WEBSITE: http://www.dfer.site
 * +----------------------------------------------------------------------
 *
 */

namespace plugins\dfer_thinkcmf_tools\controller;


/**
 * 发送验证码
 */
class ApiSendCodeController extends BaseController
{
    public function send()
    {
        if (!$this->request->isPost()) {
            $this->error(lang('illegal request'));
        }
        $validate = new \think\Validate();
        $validate->rule([
            'username' => 'require'
        ]);
        $validate->message([
            'username.require' => '请输入手机号!'
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        if (!cmf_check_mobile($data['username'])) {
            $this->error("请输入正确的手机!");
        }
        $findUserCount = db('user')->where(['mobile' => $data['username']])->where('user_status', '<>', 0)->count();
        if ($findUserCount <= 0) {
            $this->error('没有该账号！');
        }
        //限制 每个ip 的发送次数 生成数字验证码
        $code = get_verification_code($data['username']);
        if (empty($code)) {
            $this->error("验证码发送过多,请明天再试!");
        }
        if (APP_DEBUG) {
            cmf_verification_code_log($data['username'], $code, 0);
            $this->success("验证码：{$code}", null, null, 15);
        }

        $param  = ['mobile' => $data['username'], 'code' => $code];
        $result = hook_one("send_mobile_verification_code", $param);
        // var_dump($result);
        if ($result !== false && !empty($result['error'])) {
            $this->error($result['message']);
        }
        if ($result === false) {
            $this->error('未安装验证码发送插件,请联系管理员!');
        }
        $expireTime = empty($result['expire_time']) ? 0 : $result['expire_time'];

        cmf_verification_code_log($data['username'], $code, $expireTime);

        if (!empty($result['message'])) {
            $this->success($result['message']);
        } else {
            $this->success('验证码已经发送成功!');
        }
    }
}
